package com.liyuxiang.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.liyuxiang.yygh.hosp.repository.DepartmentRepository;
import com.liyuxiang.yygh.hosp.service.DepartmentService;
import com.liyuxiang.yygh.model.hosp.Department;
import com.liyuxiang.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    DepartmentRepository departmentRepository;

    @Override
    public void saveDepartment(Map<String, Object> paramMap) {
        String jsonString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(jsonString, Department.class);
        String depcode = department.getDepcode();
        String hoscode = department.getHoscode();
        Department departmentExist = departmentRepository.getDepartmentByDepcodeAndHoscode(depcode, hoscode);
        if (!ObjectUtils.isEmpty(departmentExist)) {
            department.setIsDeleted(departmentExist.getIsDeleted());
            department.setUpdateTime(new Date());
        } else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
        }
        departmentRepository.save(department);
    }

    @Override
    public Page<Department> getDepartment(String hoscode, int pageNum, int pageSize) {
        //创建Pageable，设置当前页和记录数
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Department department = new Department();
        department.setHoscode(hoscode);
        department.setIsDeleted(0);
        //创建Example对象
        ExampleMatcher matcher = ExampleMatcher.matching();
        matcher.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
        Example<Department> example = Example.of(department, matcher);
        Page<Department> page = departmentRepository.findAll(example, pageable);
        return page;
    }

    @Override
    public void removeDepartment(String depcode, String hoscode) {
        Department department = departmentRepository.getDepartmentByDepcodeAndHoscode(depcode, hoscode);
        if (!ObjectUtils.isEmpty(department)) {
            department.setIsDeleted(1);
            department.setUpdateTime(new Date());
            departmentRepository.save(department);
        }
    }

    //根据医院编号查询所有科室
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        List<DepartmentVo> result = new ArrayList<>();
        List<Department> departmentList = departmentRepository.getDepartmentByHoscode(hoscode);
        Map<String, List<Department>> departmentMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        departmentMap.entrySet().stream().forEach(e -> {
            //大科室编号
            String key = e.getKey();
            //大科室编号对应的全部数据
            List<Department> departmentList1 = e.getValue();
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(key);
            departmentVo.setDepname(departmentList1.get(0).getBigname());
            List<DepartmentVo> deptChildrenList = new ArrayList<>();
            departmentList1.stream().forEach(i -> {
                DepartmentVo childrenDept = new DepartmentVo();
                childrenDept.setDepcode(i.getDepcode());
                childrenDept.setDepname(i.getDepname());
                deptChildrenList.add(childrenDept);
            });
            departmentVo.setChildren(deptChildrenList);
            result.add(departmentVo);
        });
        return result;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);
    }
}
